<template>
  <div style="margin: 15px; margin-right:30px">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="上游客户" prop="userCode">
            <el-select v-model="form.userCode" placeholder="请选择上游客户" clearable>
              <el-option
                v-for="item in parentData.upstreamCustomerList"
                :key="item.partyCode"
                :label="item.userName"
                :value="item.partyCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="货物类型" prop="coalBigType">
            <el-select v-model="form.coalBigType" placeholder="请选择货物类型(大)" clearable>
              <el-option
                v-for="item in parentData.goodsTypeList"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="coalType" label-width="30px">
            <el-select v-model="form.coalType" placeholder="请选择货物类型(小)" clearable>
              <el-option
                v-for="item in goodsTypeList2"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="车型" prop="classificationCode">
            <el-select v-model="form.classificationCode" placeholder="请选择车型" clearable>
              <el-option
                v-for="item in parentData.vehicleDictList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="起始地" prop="shipperSubdivisionCode">
            <el-cascader
              v-model="shipperSubdivisionArray"
              :options="parentData.areaOptions"
              :props="{ value: 'countyCode', label: 'countyName', children: 'children' }"
              @change="changeShipperSubdivisionArray"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="目的地" prop="consigneeSubdivisionCode">
            <el-cascader
              v-model="consigneeSubdivisionArray"
              :options="parentData.areaOptions"
              :props="{ value: 'countyCode', label: 'countyName', children: 'children' }"
              @change="changeConsigneeSubdivisionArray"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="付费方式" prop="chargingType">
            <el-select v-model="form.chargingType" placeholder="请选择付费方式" clearable>
              <el-option
                v-for="item in chargingTypeOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="应收单价" prop="price">
            <el-input v-model="form.price" prop="address" placeholder="请输入应收单价" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="应收总价" prop="allFee">
            <el-input v-model="form.allFee" prop="address" placeholder="请输入应收总价" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
Vue.use(VXETable)
import { addUpstreamCustomer } from '@/api/shipment'
export default {
  name: 'UpstreamCustomerAdd',
  props: {
    parentData: {
      type: Object,
      default() {
        return {
          areaOptions: [],
          goodsTypeList: [],
          typeOptions: [],
          vehicleDictList: [],
          bankOptions: [],
          upstreamCustomerList: []
        }
      }
    }
  },
  data() {
    return {
      shipperSubdivisionArray: [],
      consigneeSubdivisionArray: [],
      goodsTypeList2: [],
      form: {
        companyCode: '',
        userCode: '',
        coalBigType: '',
        coalType: '',
        classificationCode: '',
        shipperSubdivisionCode: '',
        consigneeSubdivisionCode: '',
        chargingType: '',
        price: '',
        allFee: ''
      },
      chargingTypeOptions: [
        { name: '单价吨/方/件', value: 0 },
        { name: '单价吨/方/件*公里数', value: 1 },
        { name: '总价', value: 2 }
      ],
      rules: {
        userCode: [
          { required: true, message: '请选择上游客户', trigger: 'change' }
        ],
        coalBigType: [
          { required: true, message: '请选择货物类型（大）', trigger: 'change' }
        ],
        coalType: [
          { required: true, message: '请选择货物类型（小）', trigger: 'change' }
        ],
        classificationCode: [
          { required: true, message: '请选择车型', trigger: 'change' }
        ],
        shipperSubdivisionCode: [
          { required: true, message: '请选择起始地', trigger: 'change' }
        ],
        consigneeSubdivisionCode: [
          { required: true, message: '请选择目的地', trigger: 'change' }
        ],
        chargingType: [
          { required: true, message: '请选择付费方式', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入应收单价', trigger: 'blur' }
        ],
        allFee: [
          { required: true, message: '请选择应收总价', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    computedCoalBigType() {
      return this.form.coalBigType
    }
  },
  watch: {
    computedCoalBigType(val) {
      this.form.coalType = ''
      var item = this.parentData.goodsTypeList.find(item => item.code === val)
      if (item && item.children) {
        this.goodsTypeList2 = item.children
      } else {
        this.goodsTypeList2 = []
      }
    }
  },
  created() {
    this.form.companyCode = this.$route.query.code ? this.$route.query.code : "";
  },
  methods: {
    funcSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.companyCode = this.form.companyCode ? this.form.companyCode : this.parentData.upstreamCustomerList[0].companyCode;
          const rLoading = this.openLoading();
          addUpstreamCustomer(this.form)
            .then(response => {
              rLoading.close();
              this.initData()
              this.$message({ message: response.msg, type: 'success' })
              this.$emit('fatherAddMethod')
            })
            .then(() => this.$refs['form'].resetFields())
        } else {
          this.$message.error('必选项不能为空!')
        }
      })
    },
    initData() {
      this.shipperSubdivisionArray = []
      this.consigneeSubdivisionArray = []
      this.goodsTypeList2 = []
    },
    funcCancel() {
      this.$emit('fatherCancelMethod', 'add')
    },
    changeShipperSubdivisionArray(value) {
      this.form.shipperSubdivisionCode = value[2]
    },
    changeConsigneeSubdivisionArray(value) {
      this.form.consigneeSubdivisionCode = value[2]
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog-footer {
  margin: 15px 0 15px 0;
  width: 175px;
  float: right;
}
.el-cascader {
  width: 100%;
}
.el-select {
  width: 100%;
}
</style>
